//*************************************************************************************************
//************************ (C) 2001 - 2006, Eleven Engineering Incorporated ***********************
//*************************************************************************************************
//**
//**		Tabs:	This file looks best with tab stops set every 6 spaces.
//**
//*************************************************************************************************
//*************************************************************************************************
//**
//**          File:  Init.asm
//**       Project:  Quick Start Project
//**       Created:  25 Jun 2002 by Ryan Northcott
//**       Revised:  08 Sep 2006 by Dan Beutel
//**
//**   Description:  This file contains initialization code that is run on thread 0 after XInC2 is
//**                 powered on.  It releases all semaphores, sets up the Program Counters and
//**                 Stack Pointers of all threads, and then instructs the SCU to enable the
//**                 threads which are configured in the main file to be run.
//**
//** Compatibility:  Requires XInA v4.4 or higher.
//**
//**    Disclaimer:  You may incorporate this sample source code into your program(s) without
//**                 restriction.  This sample source code has been provided "AS IS" and the
//**                 responsibility for its operation is yours.  You are not permitted to
//**                 redistribute this sample source code as "Eleven sample source code" after
//**                 having made changes.  If you are going to re-distribute the source, we require
//**                 that you make it clear in the source that the code was descended from Eleven
//**                 sample source code, but that you have made changes.
//**
//*************************************************************************************************
//*************************************************************************************************

	// Program the EEPROM
		bra	0x0014 //ProgramSPIEEPROM
		0x9809

	// Clear Resource Vector (Hardware Semaphores)
		inp	r0, SCUrsrc
		outp	r0, SCUup

		__RUN_THREADS__ = 0b11111111

#ifdef __DEBUGGER__	// Setup Thread 0's Stack Pointer
		mov	r7, T0_SP
		__RUN_THREADS__ = __RUN_THREADS__ & 0b11111110
#endif

#ifdef __T0__		// Setup Thread 0's Stack Pointer
		mov	r7, T0_SP
		__RUN_THREADS__ = __RUN_THREADS__ & 0b11111110
#endif

#ifdef __T1__		// Setup Thread 1's Program Counter & Stack Pointer
		mov	r1, 7 | (1<<3)
		outp	r1, SCUpntr
		mov	r1, T1_SP
		outp	r1, SCUreg
		mov	r1, Thread1
		outp	r1, SCUpc
		__RUN_THREADS__ = __RUN_THREADS__ & 0b11111101
#endif

#ifdef __T2__		// Setup Thread 2's Program Counter & Stack Pointer
		mov	r1, 7 | (2<<3)
		outp	r1, SCUpntr
		mov	r1, T2_SP
		outp	r1, SCUreg
		mov	r1, Thread2
		outp	r1, SCUpc
		__RUN_THREADS__ = __RUN_THREADS__ & 0b11111011
#endif

#ifdef __T3__		// Setup Thread 3's Program Counter & Stack Pointer
		mov	r1, 7 | (3<<3)
		outp	r1, SCUpntr
		mov	r1, T3_SP
		outp	r1, SCUreg
		mov	r1, Thread3
		outp	r1, SCUpc
		__RUN_THREADS__ = __RUN_THREADS__ & 0b11110111
#endif

#ifdef __T4__		// Setup Thread 4's Program Counter & Stack Pointer
		mov	r1, 7 | (4<<3)
		outp	r1, SCUpntr
		mov	r1, T4_SP
		outp	r1, SCUreg
		mov	r1, Thread4
		outp	r1, SCUpc
		__RUN_THREADS__ = __RUN_THREADS__ & 0b11101111
#endif

#ifdef __T5__		// Setup Thread 5's Program Counter & Stack Pointer
		mov	r1, 7 | (5<<3)
		outp	r1, SCUpntr
		mov	r1, T5_SP
		outp	r1, SCUreg
		mov	r1, Thread5
		outp	r1, SCUpc
		__RUN_THREADS__ = __RUN_THREADS__ & 0b11011111
#endif

#ifdef __T6__		// Setup Thread 6's Program Counter & Stack Pointer
		mov	r1, 7 | (6<<3)
		outp	r1, SCUpntr
		mov	r1, T6_SP
		outp	r1, SCUreg
		mov	r1, Thread6
		outp	r1, SCUpc
		__RUN_THREADS__ = __RUN_THREADS__ & 0b10111111
#endif

#ifdef __T7__		// Setup Thread 7's Program Counter & Stack Pointer
		mov	r1, 7 | (7<<3)
		outp	r1, SCUpntr
		mov	r1, T7_SP
		outp	r1, SCUreg
		mov	r1, Thread7
		outp	r1, SCUpc
		__RUN_THREADS__ = __RUN_THREADS__ & 0b01111111
#endif

#ifndef __DEBUGGER__
		mov	r0, __RUN_THREADS__
		outp	r0, SCUstop	// Enable the desired threads
#endif